package cn.edkuir.seckill.controller;

import cn.edkuir.common.utils.R;
import cn.edkuir.seckill.service.SeckillService;
import cn.edkuir.seckill.to.SeckillRedisTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class SeckillController {
    @Autowired
    private SeckillService seckillService;


    @GetMapping("/currentSeckillSkus")
    @ResponseBody
    public R getCurrentSeckillSkus() {
        List<SeckillRedisTo> data = seckillService.getCurrentSeckillSkus();
        System.out.println("当前秒杀商品: " + data);

        return R.ok().put("data", data);
    }


    @GetMapping("/sku/seckill/{skuId}")
    @ResponseBody
    public R getSeckillSkuInfo(@PathVariable("skuId") Long skuId) {
        SeckillRedisTo data = seckillService.getSeckillSkuInfo(skuId);

        return R.ok().put("data", data);
    }


    @GetMapping("/kill")
    public String seckill(@RequestParam("killId") String killId,
                          @RequestParam("key") String key,
                          @RequestParam("num") Integer num,
                          Model model) {
        String orderSn = seckillService.kill(killId, key, num);
        model.addAttribute("orderSn", orderSn);

        return "success";
    }

}
